import 'package:flutter/material.dart';

/// 翻转组件
class FlipWidget extends StatefulWidget {
  final Widget child;
  final bool flipX;
  final bool flipY;
  final int duration;
  final Curve? curve;
  final Alignment alignment;
  final Offset? origin;
  final bool transformHitTests;
  final FilterQuality? filterQuality;
  final bool animate;

  //循环播放
  final bool repeat;

  // 反向
  final bool reverse;

  const FlipWidget({
    super.key,
    required this.child,
    this.flipX = false,
    this.flipY = false,
    this.duration = 300,
    this.curve,
    this.alignment = Alignment.center,
    this.origin,
    this.transformHitTests = true,
    this.filterQuality,
    this.animate = false,
    this.repeat = false,
    this.reverse = false,
  });

  @override
  _FlipWidgetState createState() => _FlipWidgetState();
}

class _FlipWidgetState extends State<FlipWidget> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animationX;
  late Animation<double> _animationY;
  late double _previousScaleX;
  late double _previousScaleY;

  // 目标翻转缩放值：水平翻转时scaleX为-1，否则为1
  double get _targetScaleX => widget.flipX ? -1.0 : 1.0;

  // 目标翻转缩放值：垂直翻转时scaleY为-1，否则为1
  double get _targetScaleY => widget.flipY ? -1.0 : 1.0;

  @override
  void initState() {
    super.initState();
    _previousScaleX = _targetScaleX;
    _previousScaleY = _targetScaleY;
    _controller = AnimationController(
      duration: Duration(milliseconds: widget.duration),
      vsync: this,
    );

    // 创建X轴翻转动画
    _animationX = Tween<double>(begin: _previousScaleX, end: _targetScaleX).animate(
      widget.curve == null ? _controller : CurvedAnimation(parent: _controller, curve: widget.curve!),
    );

    // 创建Y轴翻转动画
    _animationY = Tween<double>(begin: _previousScaleY, end: _targetScaleY).animate(
      widget.curve == null ? _controller : CurvedAnimation(parent: _controller, curve: widget.curve!),
    );

    _start();
  }

  void _start() {
    if (widget.animate) {
      if (widget.repeat) {
        _controller.repeat(reverse: widget.reverse);
      } else {
        _controller.forward();
      }
    }
  }

  @override
  void didUpdateWidget(FlipWidget oldWidget) {
    super.didUpdateWidget(oldWidget);

    // 当翻转状态、持续时间或动画曲线发生变化时重置动画
    if (widget.animate && (widget.flipX != oldWidget.flipX || widget.flipY != oldWidget.flipY || widget.duration != oldWidget.duration || widget.curve != oldWidget.curve)) {
      _previousScaleX = _animationX.value;
      _previousScaleY = _animationY.value;
      _controller
        ..duration = Duration(milliseconds: widget.duration)
        ..reset();
      _start();
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.animate) {
      // 动画模式下使用AnimatedBuilder构建翻转动画
      return AnimatedBuilder(
        animation: _controller,
        builder: (context, child) {
          return Transform(
            transform: Matrix4.identity()..scale(_animationX.value, _animationY.value),
            alignment: widget.alignment,
            origin: widget.origin,
            transformHitTests: widget.transformHitTests,
            filterQuality: widget.filterQuality,
            child: child,
          );
        },
        child: widget.child,
      );
    } else {
      // 非动画模式下直接应用翻转变换
      return Transform(
        transform: Matrix4.identity()..scale(_targetScaleX, _targetScaleY),
        alignment: widget.alignment,
        origin: widget.origin,
        transformHitTests: widget.transformHitTests,
        filterQuality: widget.filterQuality,
        child: widget.child,
      );
    }
  }
}
